package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TwlStockOut;
import com.ys.entity.hr.TwlStockOutDetail;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.TwlStockOutService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/twlStockOut")
public class ApiInterTwlStockOutController {
	
	@Autowired
	private CommonApiService<TwlStockOut> commonApiService;
	@Autowired
	private CommonApiService<TwlStockOutDetail> commonApiServiceDetail;
	@Autowired
	private TwlStockOutService twlStockOutService;
	
	@RequiresPermissions("twlStockOut:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TwlStockOutDetail cond){
		
		cond = setSeachSql(cond);
		
		cond.addField("(SELECT ISNULL(SUM(Num),0) FROM TWL_StockBorrowDetail AA LEFT JOIN TWL_StockBorrow BB ON BB.Bid = AA.BidStockBorrow WHERE BB.EffectStock = 1 AND AA.StockId = A.StockId) numBorrow");
		
		cond.setOrderby("A.Id ASC");
		return commonApiServiceDetail.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("twlStockOut:add")
	public R add(String bid, String remark, String subData) {
		try {
			//修改
			return twlStockOutService.add(bid, remark, subData, MenuId.TWL_StockOut);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("twlStockOut:add")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(String bid, String remark, String subData){
		try {
			return twlStockOutService.edit(bid, remark, subData, MenuId.TWL_StockOut);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("twlStockOut:add")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return twlStockOutService.flowPass(id, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("twlStockOut:add")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		try {
			return twlStockOutService.flowRefuse(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}


	/**
	 * 流转驳回
	 */
	@RequiresPermissions("twlStockOut:add")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){
		try {
			return twlStockOutService.flowInit(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return twlStockOutService.deleteTrue(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("twlStockOut:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TwlStockOut cond) throws Exception{
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TWL_StockOut)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TwlStockOut> util = new ExportExcelUtils<TwlStockOut>(new TwlStockOut());
        
        String toFilePath = util.exportExcel(listData, listField, "物料出库单");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TwlStockOutDetail setSeachSql(TwlStockOutDetail cond){
		
		cond.addLeftJoin("TWL_StockOut B ON B.Bid = A.BidStockOut")
			.addField("B.Bid bid")
			.addField("B.Remark remark")
			.addField("B.EffectStock effectStock");
    	
    	cond.addLeftJoin("YS_ProcInst C ON C.Bid = B.Bid")
			.addField("C.UpdateTime auditTime")
			.addField("C.CreateUserId createUserId");
		
		cond.addLeftJoin("TWL_Stock D ON D.Id = A.StockId")
			.addField("D.Num stockNum")
			.addField("D.StockInDay stockInDay")
			.addField("DATEDIFF(DAY, D.StockInDay , getdate()) stockInDays")
			.addField("D.Remark stockRemark");
		
		cond.addLeftJoin("TWL_StockInDetail D1 ON D1.Id = D.StockInDetailId")
			.addField("D1.BidStockIn bidStockIn")
			.addField("D1.Img img")
			.addField("D1.Price price");
		
		cond.addLeftJoin("TB_Material D2 ON D2.Code = D.MaterialCode")
			.addField("D2.Code materialCode")
			.addField("D2.Name materialName")
			.addField("D2.Remark materialRemark");
		
		cond.addLeftJoin("TS_OrganizationInfo D3 ON D3.Orgid = D.Orgid")
			.addField("D3.OrgName orgName")
			.addField("D3.HeadUserId orgHeadUserId");
		
		cond.addLeftJoin("TB_CompanyInfo D4 ON D4.Vid = D.CompanyVid")
			.addField("D4.Vname companyVname");
		
        return  cond;
    }
}
